package com.ghxxzx.daglcustom.business_delegate.web;

import com.ghxxzx.daglcustom.business_delegate.domain.DelegatePo;
import com.ghxxzx.daglcustom.business_delegate.domain.dto.DelegateDto;
import com.ghxxzx.daglcustom.business_delegate.domain.dto.DelegateQuery;
import com.ghxxzx.daglcustom.business_delegate.service.DelegateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.tmsps.ne4spring.base.BaseAction;
import org.tmsps.ne4spring.base.ne.NePage;
import org.tmsps.ne4spring.utils.wrapper.WrapMapper;
import org.tmsps.ne4spring.utils.wrapper.Wrapper;

import javax.validation.Valid;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/delegate")
public class DelegateAction extends BaseAction {

	@Autowired
	private DelegateService delegateService;
	
	@GetMapping("/list")
	public Wrapper<NePage> list(@Valid DelegateQuery delegateQuery) throws ParseException {
		NePage page = delegateService.list(delegateQuery);
		return WrapMapper.ok(page);
	}
	
	@PostMapping("/add")
	public Wrapper<String> add(@Valid DelegateDto delegateDto) throws ParseException {
		DelegatePo delegatePo = new DelegatePo();
		delegatePo.setBusiness(delegateDto.getBusiness());
		delegatePo.setUser(delegateDto.getUser());
		delegatePo.setUserd(delegateDto.getUserd());
		SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
		Date startDate = format1.parse(delegateDto.getStart());
		Date endDate = format1.parse(delegateDto.getEnd());
		delegatePo.setStart(startDate.getTime());
		delegatePo.setEnd(endDate.getTime());
		delegatePo.setStatuz(delegateDto.getStatuz());
		delegatePo.setReason(delegateDto.getReason());
		bs.saveObj(delegatePo);
		return WrapMapper.ok("添加成功");
	}
	
	@PostMapping("/edit_form")
	public Wrapper<DelegatePo> editForm(@Valid String kid) {
		DelegatePo delegatePo = bs.findById(kid, DelegatePo.class);
		return WrapMapper.ok(delegatePo);
	}
	
	@PostMapping("/edit")
	public Wrapper<String> edit(@Valid DelegateDto delegateDto) throws ParseException {
		DelegatePo delegatePo = bs.findById(delegateDto.getKid(), DelegatePo.class);
		delegatePo.setBusiness(delegateDto.getBusiness());
		delegatePo.setUser(delegateDto.getUser());
		delegatePo.setUserd(delegateDto.getUserd());
		SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
		Date startDate = format1.parse(delegateDto.getStart());
		Date endDate = format1.parse(delegateDto.getEnd());
		delegatePo.setStart(startDate.getTime());
		delegatePo.setEnd(endDate.getTime());
		delegatePo.setStatuz(delegateDto.getStatuz());
		delegatePo.setReason(delegateDto.getReason());
		bs.updateObj(delegatePo);
		return WrapMapper.ok("修改成功");
	}
	
	@GetMapping("/delete")
	public Wrapper<String> delete(@Valid String kid) {
		bs.deleteById(kid, DelegatePo.class);
		return WrapMapper.ok("删除成功");
	}
	
}
